package strategy.demo;

import java.util.Arrays;

//proxy 代理模式
public class SortContext {
    public void sort(int[] arr) {
        if (sortStrategy != null) {
            System.err.println("Using " + sortStrategy.getClass().getName() + " sort strategy");
            System.err.println("array size ="  + arr.length);
            System.err.println("before sorting: the array is " + Arrays.toString(arr));
            long start = System.currentTimeMillis();

            sortStrategy.sort(arr);//委托

            System.err.println("after sorted: the array is " + Arrays.toString(arr));
            System.err.println("It costed " + (System.currentTimeMillis() - start) / (1000.0) + " second(s).");
        } else {
            System.err.println("sortStrategy is null");
        }
    }

    public SortContext(SortStrategy sortStrategy) {
        this.sortStrategy = sortStrategy;
    }

    SortStrategy sortStrategy;

    public void setSortStrategy(SortStrategy sortStrategy) {
        this.sortStrategy = sortStrategy;
    }
}
